<?php
/**
 * Necro Model for Congregation Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * Necro Model
 *
 * @package    Congregation
 * @subpackage Components
 */
class CongregationModelNecro extends CongregationModel
{
	/**
	 * Constructor that retrieves the ID from the request
	 *
	 * @access	public
	 * @return	void
	 */
	 

	function __construct()
	{
		parent::__construct();
		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	/**
	 * Method to set the necro identifier
	 *
	 * @access	public
	 * @param	int Sharing identifier
	 * @return	void
	 */
	function setId($id)
	{
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
		$this->_datalist = null;
	}

	/**
	 * Method to get a sharing
	 * @return object with data
	 */
	function &getData()
	{
		// Load the data
		if (empty( $this->_data )) {
			$query = " SELECT n.*, m.fname, m.lname, m.ddate, m.dplace, s.abbr FROM #__congregation_necro n 
			LEFT JOIN #__congregation_profile m ON n.username=m.username
			LEFT JOIN #__congregation_status AS s ON m.status=s.id 
			WHERE n.id = " . $this->_id ;
			
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
			
		}
		if (!$this->_data) {
			$this->_data = & JTable::getInstance('necro', 'Table');
		}
		return $this->_data;
	}

	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store()
	{	
		$row =& $this->getTable();

		$data = JRequest::get( 'post', 4 );
		
		$data['date'] = $data['d_year'] . '-' . $data['d_month'] . '-' .$data['d_day'];
		
		// Bind the form fields to the sharing table
		if (!$row->bind($data)) {
			
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// if new item, order last in appropriate group
		/*
		if (!$row->id) {

			$row->ordering = $row->getNextOrder();
		}
		*/
		// Make sure the sharing record is valid
		if (!$row->check()) {
			JError::raiseWarning( 500, $this->_db->getError());
			return false;
		}

		// Store the web link table to the database
		if (!$row->store()) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}

		//DELETE the user from user table
		$query = "DELETE FROM #__users WHERE username = '" . $data['username'] . "' LIMIT 1";
		$this->_db->setQuery( $query );
		$this->_db->query();
		return true;
	}

	/**
	 * Method to delete record(s)
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function delete()
	{
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		$row =& $this->getTable();

		if (count( $cids )) {
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
			}
		}
		return true;
	}
	
	function &getList()
	{
		if (empty( $this->_datalist ))
		{
			$query = " SELECT n.*, m.fname, m.lname, m.ddate, m.dplace, s.abbr FROM #__congregation_necro n 
			LEFT JOIN #__congregation_profile m ON n.username=m.username
			LEFT JOIN #__congregation_status AS s ON m.status=s.id 
			ORDER BY n.ordering ";
			
			$this->_db->setQuery( $query );
			$this->_datalist = $this->_getList( $query );
		}
		return $this->_datalist;
	}

	function &getPassedUsers()
	{
		$query = "SELECT m.lname, m.fname, u.username FROM #__congregation_profile m LEFT JOIN #__users u ON m.username = u.username WHERE m.ddate <> '0000-00-00' AND u.username IS NOT NULL";
		$this->_db->setQuery( $query );
		return $this->_getList( $query );
	}
}